﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Configuration;
using System.IO;
using System.Data;
using System.Text;
using System.Xml.Serialization;


namespace Com.FoxGlobal.Daniel.Midea.TemplateService
{
    [Serializable]
    public class MetaDataSettings
    {
        public static string SETTING_PATH =  "MetaTables.xml";

        public List<MetaTable> MetaTables;

        private MetaDataSettings()
        {
            MetaTables = new List<MetaTable>();
        }

        public void Save()
        {
            XmlSerializer xs = new XmlSerializer(typeof(MetaDataSettings));
            Stream stream = new FileStream(SETTING_PATH, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
            xs.Serialize(stream, this);
            stream.Close();
        }

        public static MetaDataSettings Load(string path="")
        {
            try
            {
                path = (path == "") ? SETTING_PATH : path;
                XmlSerializer xs = new XmlSerializer(typeof(MetaDataSettings));
                Stream stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                MetaDataSettings p = (MetaDataSettings)xs.Deserialize(stream);
                stream.Close();
                return p;
            }
            catch (Exception ex)
            {
                Logging.WriteLog (LoggingLevel.ERROR, "Exception occur while load metadata settings. {0}", ex.ToString());
                MetaDataSettings settings = new MetaDataSettings();
                settings.Save();
                return settings;
            }
        }
    }

    [Serializable]
    public class MetaTable
    {
        public string Name;
        public List<MetaFiled> MetaFileds;

        public MetaTable()
        {
            Name = "";
            MetaFileds = new List<MetaFiled>();
        }
    }

    [Serializable]
    public class MetaFiled
    {
        public String Name;
        public String FieldType;
        public String Requried;
        public String MetaDataTable;
        public String MetaDataFiled;
        public String Filter;

        public MetaFiled()
        {
            Name = "";
            FieldType = "";
            Requried = "";
            MetaDataTable = "";
            MetaDataFiled = "";
            Filter = "";
        }
    }
    
}